package com.dengdai.applibrary.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dengdai.applibrary.R;
import com.dengdai.applibrary.base.BaseApplication;

/**
 * @说明 自动消失对话框
 * @作者 LY
 * @时间 2016/1/5 9:51
 * @版权 Copyright(c) 2016 LY-版权所有
 */
public class CustomDismissDialog extends Dialog {
    private TextView tv_title;
    private TextView tv_content;

    public CustomDismissDialog(Context context) {
        super(context);
        initView();
    }

    /**
     * 初始化界面
     */
    public void initView() {
        View view = LayoutInflater.from(BaseApplication.getAppContext()).inflate(R.layout.common_custom_dismiss_dialog, null);
        tv_title = (TextView) view.findViewById(R.id.tv_title);
        tv_content = (TextView) view.findViewById(R.id.tv_content);
        //设置没有标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //设置动画
        getWindow().setWindowAnimations(R.style.dialogAnim);
        //设置背景颜色
        getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        addContentView(view, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
    }

    /**
     * 显示信息对话框  3秒后自动消失
     *
     * @param title        标题
     * @param content      内容
     * @param isCancelable 是否可取消的
     */
    public void showInfoDialog(int title, int content, boolean isCancelable) {
        showInfoDialog(BaseApplication.getAppContext().getResources().getString(title),
                BaseApplication.getAppContext().getResources().getString(content),
                isCancelable);
    }

    /**
     * 显示信息对话框 3秒后自动消失
     *
     * @param title        标题
     * @param content      内容
     * @param isCancelable 是否可取消的
     */
    public void showInfoDialog(String title, String content, boolean isCancelable) {
        tv_title.setText(title);
        tv_content.setText(content);
        setCancelable(isCancelable);
        show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                dismiss();
            }
        }, 3000);
    }

}
